package com.smartlazy.sequence.server.web;

import com.alibaba.fastjson.JSONObject;
import com.smartlazy.sequence.server.bean.ReturnInfo;
import com.smartlazy.sequence.server.service.IDServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @author howe
 */
@Slf4j
@Controller
@RequestMapping("/seq")
public class SequenceController {

    @Autowired
    private IDServer idServer;

    /**
     * redisRange
     *
     * @return
     */
    @RequestMapping(value = "/redisRange", method = RequestMethod.POST)
    @ResponseBody
    public ReturnInfo<Long> redisRange() {
        ReturnInfo ret = new ReturnInfo();
        try {
            ret.setData(idServer.nextMerchantId());
            ret.setErrorCode("200");
            ret.setErrorText("成功");
            ret.setReturnCode(1);
        } catch (Exception e) {
            ret.setErrorCode("500");
            ret.setErrorText("系统异常");
            ret.setReturnCode(2);
            log.error("系统异常", e);
        }
        return ret;
    }

    /**
     * redisNoRange
     *
     * @return
     */
    @RequestMapping(value = "/redisNoRange", method = RequestMethod.POST)
    @ResponseBody
    public ReturnInfo<Long> redisNoRange() {
        ReturnInfo ret = new ReturnInfo();
        try {
            ret.setData(idServer.nextSerNo());
            ret.setErrorCode("200");
            ret.setErrorText("成功");
            ret.setReturnCode(1);
        } catch (Exception e) {
            ret.setErrorCode("500");
            ret.setErrorText("系统异常");
            ret.setReturnCode(2);
            log.error("系统异常", e);
        }
        return ret;
    }

    /**
     * redisRangeByName
     *
     * @return
     */
    @RequestMapping(value = "/redisRangeByName", method = RequestMethod.POST)
    @ResponseBody
    public ReturnInfo<Long> redisRangeByName(@RequestBody String name) {
        ReturnInfo ret = new ReturnInfo();
        try {
            JSONObject input = JSONObject.parseObject(name);
            ret.setData(idServer.nextAccId(input.getString("name")));
            ret.setErrorCode("200");
            ret.setErrorText("成功");
            ret.setReturnCode(1);
        } catch (Exception e) {
            ret.setErrorCode("500");
            ret.setErrorText("系统异常");
            ret.setReturnCode(2);
            log.error("系统异常", e);
        }
        return ret;
    }

    /**
     * mysqlRange
     *
     * @return
     */
    @RequestMapping(value = "/mysqlRange", method = RequestMethod.POST)
    @ResponseBody
    public ReturnInfo<Long> mysqlRange(@RequestBody String name) {
        ReturnInfo ret = new ReturnInfo();
        try {
            JSONObject input = JSONObject.parseObject(name);
            ret.setData(idServer.nextMysqlId(input.getString("name")));
            ret.setErrorCode("200");
            ret.setErrorText("成功");
            ret.setReturnCode(1);
        } catch (Exception e) {
            ret.setErrorCode("500");
            ret.setErrorText("系统异常");
            ret.setReturnCode(2);
            log.error("系统异常", e);
        }
        return ret;
    }
}
